package com.winfar.ic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Environment;

public class FileHelper {
	private static Context mContext;
	private static String SDPATH;
	private static String FILESPATH;

	public static void setContext(Context context) {
		mContext = context.getApplicationContext();
		File file = mContext.getFilesDir();
		FILESPATH = file.getPath() + "\\";

		File skRoot = Environment.getExternalStorageDirectory();
		SDPATH = skRoot.getPath() + "\\";
	}

	/**
	 * 是否存在SD卡
	 * 
	 * @return
	 */
	public static boolean hasSDCard() {
		return Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
	}

	/**
	 * 目录是否存在
	 */
	public static boolean hasDir(String dirPathName) {
		File dir = new File(dirPathName);
		return dir.exists();
	}

	/**
	 * 文件是否存在
	 */
	public static boolean hasFile(String filePathName) {
		File file = new File(filePathName);
		return file.exists();
	}

	/**************************SD卡上的操作***********************/
	/**
	 * 在SD卡上创建文件
	 */
	public static File creatSDFile(String fileName) throws IOException {
		return FileHelper.creatFile(SDPATH + fileName);
	}
	/**
	 * 删除SD卡上的文件
	 */
	public static boolean delSDFile(String fileName) {
		return FileHelper.delFile(SDPATH + fileName);
	}
	/**
	 * 在SD卡上创建目录
	 */
	public static File creatSDDir(String dirName) {
		return FileHelper.creatDir(SDPATH + dirName);
	}
	/**
	 * 删除SD卡上的目录
	 */
	public static boolean delSDDir(String dirName) {
		return FileHelper.delDir(SDPATH + dirName);
	}
	/**
	 * 修改SD卡上的文件或目录名
	 */
	public static boolean renameSDFile(String oldfileName, String newFileName) {
		return FileHelper
				.renameFile(SDPATH + oldfileName, SDPATH + oldfileName);
	}
	/**
	 * 拷贝SD卡上的单个文件
	 */
	public static boolean copySDFileTo(String srcFileName, String destFileName)
			throws IOException {
		return FileHelper.copyFilesTo(SDPATH + srcFileName, SDPATH
				+ destFileName);
	}
	/**
	 * 拷贝SD卡上指定目录的所有文件
	 */
	public static boolean copySDFilesTo(String srcDirName, String destDirName)
			throws IOException {
		return copySDFilesTo(SDPATH + srcDirName, SDPATH + srcDirName);
	}
	/**
	 * 移动SD卡上的单个文件
	 */
	public static boolean moveSDFileTo(String srcFileName, String destFileName)
			throws IOException {
		return FileHelper.moveFilesTo(SDPATH + srcFileName, SDPATH
				+ destFileName);
	}
	/**
	 * 移动SD卡上的指定目录的所有文件
	 */
	public static boolean moveSDFilesTo(String srcDirName, String destDirName)
			throws IOException {
		return FileHelper
				.moveFilesTo(SDPATH + srcDirName, SDPATH + destDirName);
	}
	/*
	 * 将文件写入sd卡。如:writeSDFile("test.txt","hello");
	 */
	public static void writeSDFile(String fileName, String content)
			throws IOException {
		File file = new File(SDPATH + fileName);
		FileOutputStream fos = new FileOutputStream(file);
		FileHelper.writeFile(fos, content);
	}
	/*
	 * 在原有文件上继续写文件。如:appendSDFile("test.txt","hello");
	 */
	public static void appendSDFile(String fileName, String content)
			throws IOException {
		File file = new File(SDPATH + fileName);
		FileOutputStream fos = new FileOutputStream(file, true);
		FileHelper.writeFile(fos, content);
	}
	/*
	 * 从SD卡读取文件。如:readSDFile("test.txt");
	 */
	public static byte[] readSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		FileInputStream fis = new FileInputStream(file);
		return FileHelper.readFile(fis);
	}
	
	/************************私有目录文件的操作*********************/
	/**
	 * 建立私有文件
	 */
	public static File creatDataFile(String fileName) throws IOException {
		return FileHelper.creatFile(FILESPATH + fileName);
	}
	/**
	 * 建立私有目录
	 */
	public static File creatDataDir(String dirName) {
		return FileHelper.creatDir(FILESPATH + dirName);
	}
	/**
	 * 删除私有文件
	 */
	public static boolean delDataFile(String fileName) {
		return FileHelper.delFile(FILESPATH + fileName);
	}
	/**
	 * 删除私有目录
	 */
	public static boolean delDataDir(String dirName) {
		return FileHelper.delFile(FILESPATH + dirName);
	}
	/**
	 * 更改私有文件或目录名
	 */
	public static boolean renameDataFile(String oldName, String newName) {
		return FileHelper.renameFile(FILESPATH + oldName, FILESPATH + newName);
	}
	/**
	 * 在私有目录下进行文件复制
	 */
	public static boolean copyDataFileTo(String srcFileName, String destFileName)
			throws IOException {
		return FileHelper.copyFileTo(FILESPATH + srcFileName, FILESPATH
				+ destFileName);
	}
	/**
	 * 复制私有目录里指定目录的所有文件
	 */
	public static boolean copyDataFilesTo(String srcDirName, String destDirName)
			throws IOException {
		return FileHelper.copyFilesTo(FILESPATH + srcDirName, FILESPATH
				+ destDirName);
	}
	/**
	 * 移动私有目录下的单个文件
	 */
	public static boolean moveDataFileTo(String srcFileName, String destFileName)
			throws IOException {
		return FileHelper.moveFileTo(FILESPATH + srcFileName, FILESPATH
				+ destFileName);
	}
	/**
	 * 移动私有目录下的指定目录下的所有文件
	 */
	public static boolean moveDataFilesTo(String srcDirName, String destDirName)
			throws IOException {
		return FileHelper.moveFilesTo(FILESPATH + srcDirName, FILESPATH
				+ destDirName);
	}
	/*
	 * 将文件写入应用私有的files目录。如:writeFile("test.txt","hello");
	 */
	public static void wirteFile(String fileName, String content)
			throws IOException {
		OutputStream os = mContext.openFileOutput(fileName,
				Context.MODE_WORLD_WRITEABLE);
		FileHelper.writeFile(os, content);
	}
	/*
	 * 在原有文件上继续写文件。如:appendFile("test.txt","hello");
	 */
	public static void appendFile(String fileName, String content)
			throws IOException {
		OutputStream os = mContext
				.openFileOutput(fileName, Context.MODE_APPEND);
		FileHelper.writeFile(os, content);
	}
	/*
	 * 从应用的私有目录files读取文件。如:readFile("test.txt");
	 */
	public static byte[] readFile(String fileName) throws IOException {
		InputStream is = mContext.openFileInput(fileName);
		return FileHelper.readFile(is);
	}

	/**
	 * 创建文件
	 * 
	 * @param filePathName
	 * @throws IOException
	 */
	public static File creatFile(String filePathName) throws IOException {
		File file = new File(filePathName);
		file.createNewFile();
		return file;
	}

	/**
	 * 删除文件
	 * 
	 * @param filePathName
	 */
	public static boolean delFile(String filePathName) {
		File file = new File(filePathName);
		if (file == null || !file.exists() || file.isDirectory())
			return false;
		file.delete();
		return true;
	}

	/**
	 * 创建目录
	 * 
	 * @param dirPathName
	 */
	public static File creatDir(String dirPathName) {
		File dir = new File(dirPathName);
		dir.mkdir();
		return dir;
	}

	/**
	 * 删除目录
	 * 
	 * @param dirPathName
	 */
	public static boolean delDir(String dirPathName) {
		File dir = new File(dirPathName);
		return delDir(dir);
	}

	/**
	 * 修改文件或目录名
	 * 
	 * @param filePathName
	 */
	public static boolean renameFile(String oldFilePathName,
			String newFilePathName) {
		File oleFile = new File(oldFilePathName);
		File newFile = new File(newFilePathName);
		return oleFile.renameTo(newFile);
	}

	/**
	 * 拷贝SD卡上的单个文件
	 * 
	 * @param srcFilePathName
	 * @param destFilePathName
	 * @throws IOException
	 */
	public static boolean copyFileTo(String srcFilePathName,
			String destFilePathName) throws IOException {
		File srcFile = new File(srcFilePathName);
		File destFile = new File(destFilePathName);
		return copyFileTo(srcFile, destFile);
	}

	/**
	 * 拷贝SD卡上指定目录的所有文件
	 * 
	 * @param srcDirPathName
	 * @param destDirPathName
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFilesTo(String srcDirPathName,
			String destDirPathName) throws IOException {
		File srcDir = new File(srcDirPathName);
		File destDir = new File(destDirPathName);
		return copyFilesTo(srcDir, destDir);
	}

	/**
	 * 移动单个文件
	 * 
	 * @param srcFilePathName
	 * @param destFilePathName
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFileTo(String srcFilePathName,
			String destFilePathName) throws IOException {
		File srcFile = new File(srcFilePathName);
		File destFile = new File(destFilePathName);
		return moveFileTo(srcFile, destFile);
	}

	/**
	 * 移动指定目录的所有文件
	 * 
	 * @param srcDirPathName
	 * @param destDirPathName
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFilesTo(String srcDirPathName,
			String destDirPathName) throws IOException {
		File srcDir = new File(srcDirPathName);
		File destDir = new File(destDirPathName);
		return moveFilesTo(srcDir, destDir);
	}

	/*
	 * 将文件写入文件。如:writeFile("test.txt","hello");
	 */
	public static void writeFile(OutputStream os, String content)
			throws IOException {
		os.write(content.getBytes());
	}

	/*
	 * 在原有文件上继续写文件。如:appendSDFile("test.txt","hello");
	 */
	public static void appendFile(OutputStream os, String content)
			throws IOException {
		os.write(content.getBytes());
	}

	/*
	 * 读取文件。如:readFile("test.txt");
	 */
	public static byte[] readFile(InputStream is) throws IOException {
		byte[] b = new byte[is.available()];
		is.read(b);
		return b;
	}

	/**
	 * 删除一个文件
	 * 
	 * @param file
	 * @return
	 */
	public static boolean delFile(File file) {
		if (file.isDirectory())
			return false;
		return file.delete();
	}

	/**
	 * 删除一个目录（可以是非空目录）
	 * 
	 * @param dir
	 */
	public static boolean delDir(File dir) {
		if (dir == null || !dir.exists() || dir.isFile()) {
			return false;
		}
		for (File file : dir.listFiles()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				delDir(file);// 递归
			}
		}
		dir.delete();
		return true;
	}

	/**
	 * 拷贝一个文件,srcFile源文件，destFile目标文件
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static boolean copyFileTo(File srcFile, File destFile)
			throws IOException {
		if (srcFile.isDirectory() || destFile.isDirectory())
			return false;// 判断是否是文件
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);
		int readLen = 0;
		byte[] buf = new byte[1024];
		while ((readLen = fis.read(buf)) != -1) {
			fos.write(buf, 0, readLen);
		}
		fos.flush();
		fos.close();
		fis.close();
		return true;
	}

	/**
	 * 拷贝目录下的所有文件到指定目录
	 * 
	 * @param srcDir
	 * @param destDir
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFilesTo(File srcDir, File destDir)
			throws IOException {
		if (!srcDir.isDirectory() || !destDir.isDirectory())
			return false;// 判断是否是目录
		if (!destDir.exists())
			return false;// 判断目标目录是否存在
		File[] srcFiles = srcDir.listFiles();
		for (int i = 0; i < srcFiles.length; i++) {
			if (srcFiles[i].isFile()) {
				// 获得目标文件
				File destFile = new File(destDir.getPath() + "\\"
						+ srcFiles[i].getName());
				copyFileTo(srcFiles[i], destFile);
			} else if (srcFiles[i].isDirectory()) {
				File theDestDir = new File(destDir.getPath() + "\\"
						+ srcFiles[i].getName());
				copyFilesTo(srcFiles[i], theDestDir);
			}
		}
		return true;
	}

	/**
	 * 移动一个文件
	 * 
	 * @param srcFile
	 * @param destFile
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFileTo(File srcFile, File destFile)
			throws IOException {
		boolean iscopy = copyFileTo(srcFile, destFile);
		if (!iscopy)
			return false;
		delFile(srcFile);
		return true;
	}

	/**
	 * 移动目录下的所有文件到指定目录
	 * 
	 * @param srcDir
	 * @param destDir
	 * @return
	 * @throws IOException
	 */
	public static boolean moveFilesTo(File srcDir, File destDir)
			throws IOException {
		if (!srcDir.isDirectory() || !destDir.isDirectory()) {
			return false;
		}
		File[] srcDirFiles = srcDir.listFiles();
		for (int i = 0; i < srcDirFiles.length; i++) {
			if (srcDirFiles[i].isFile()) {
				File oneDestFile = new File(destDir.getPath() + "\\"
						+ srcDirFiles[i].getName());
				moveFileTo(srcDirFiles[i], oneDestFile);
				delFile(srcDirFiles[i]);
			} else if (srcDirFiles[i].isDirectory()) {
				File oneDestFile = new File(destDir.getPath() + "\\"
						+ srcDirFiles[i].getName());
				moveFilesTo(srcDirFiles[i], oneDestFile);
				delDir(srcDirFiles[i]);
			}
		}
		return true;
	}
}
